#!/usr/bin/env python3
"""
简单的GraphRAG系统测试
"""

import requests
import time

def test_server():
    """测试服务器是否运行"""
    print("🔍 测试GraphRAG Web系统...")
    print("=" * 50)
    
    # 等待服务器启动
    print("⏳ 等待服务器启动...")
    time.sleep(3)
    
    # 测试主页
    try:
        print("📡 测试主页访问...")
        response = requests.get("http://localhost:8000/", timeout=10)
        if response.status_code == 200:
            print("✅ 主页访问成功!")
            print(f"   响应长度: {len(response.text)} 字符")
        else:
            print(f"❌ 主页访问失败: {response.status_code}")
    except Exception as e:
        print(f"❌ 无法访问主页: {e}")
        return False
    
    # 测试API文档
    try:
        print("📡 测试API文档...")
        response = requests.get("http://localhost:8000/docs", timeout=10)
        if response.status_code == 200:
            print("✅ API文档访问成功!")
        else:
            print(f"❌ API文档访问失败: {response.status_code}")
    except Exception as e:
        print(f"❌ 无法访问API文档: {e}")
    
    # 测试查询接口
    try:
        print("📡 测试查询接口...")
        test_data = {
            "query": "请介绍一下苹果公司",
            "method": "local",
            "response_type": "text",
            "community_level": 1,
            "dynamic_community_selection": False
        }
        
        response = requests.post(
            "http://localhost:8000/query",
            json=test_data,
            timeout=60  # 给更多时间处理查询
        )
        
        if response.status_code == 200:
            result = response.json()
            print("✅ 查询接口测试成功!")
            print(f"   响应长度: {len(result.get('response', ''))} 字符")
            if 'context' in result:
                print(f"   上下文信息: 已包含")
        else:
            print(f"❌ 查询接口测试失败: {response.status_code}")
            print(f"   错误信息: {response.text}")
            
    except Exception as e:
        print(f"❌ 查询接口测试异常: {e}")
    
    print("\n🎉 测试完成!")
    print("💡 提示:")
    print("   • 访问 http://localhost:8000 使用Web界面")
    print("   • 访问 http://localhost:8000/docs 查看API文档")
    
    return True

if __name__ == "__main__":
    test_server() 